package com.tmt.annotation.parser;

import com.tmt.annotation.LazyLoadDefine;
import com.tmt.annotation.LazyLoadMapping;

/**
 * 作者：ThreeManTeam
 * 创建时间：2019-05-29 09:52
 */
public class LazyLoadParser {

    public static String[] getLazyLoadProperties(Class<?> clazz, String name) throws Exception {
        LazyLoadDefine annotation = clazz.getAnnotation(LazyLoadDefine.class);
        if (annotation == null)
            throw new Exception(String.format("在类%s上未找到LazyLoadDefine注解", clazz.getName()));

        String[] properties = null;
        for (LazyLoadMapping lazyLoadMapping : annotation.value()) {
            if (lazyLoadMapping.name().equals(name)) {
                properties = lazyLoadMapping.value();
                break;
            }
        }


        if (properties == null)
            throw new Exception(String.format("类%s上标注的LazyLoadDefine注解中不存在名称为%s的LazyLoadMapping注解项",
                    clazz.getName(), name));

        if (properties.length <= 0)
            throw new Exception(String.format("类%s上标注的LazyLoadDefine注解中名称为%s的LazyLoadMapping注解项，其值是空的",
                    clazz.getName(), name));

        return properties;
    }

    public static Class getValidClass(Class clazz) {
        Class result = clazz;
        try {
            String clazzName = clazz.getName().split("_\\$\\$_")[0];
            result = Class.forName(clazzName);
        } catch (Exception ex) {
        }
        return result;
    }

    public static String getValidSimpleClassName(Class clazz) {
        return clazz.getSimpleName().split("_\\$\\$_")[0];
    }

    public static String getValidClassName(Class clazz) {
        return clazz.getName().split("_\\$\\$_")[0];
    }
}
